import { StorageKey, EventKey, OtherKey } from "../common/Key";
import { GameState } from "./GameState";
import { MAX_TOADY_AD_DIAMOND_COUNT, MAX_TOADY_SHARE_DIAMOND_COUNT, MAX_TOADY_SHARE_ENERGY_COUNT } from "./GameDefine";
export const GameData = {
    _guided: false,// 新手指导
    _currCoin: '0',// 当前金币数量
    _allCon: '0',// 总获得的金币
    _diamond: 0,// 钻石数量
    _currAnimalIndex: 0,// 当前解锁动物等级
    _maxAnimalCount: 0,// 最大动物数量 

    _aliveAnimalData: [],// 场上的动物信息
    _animalBuyCount: [],// 动物购买次数

    _signDay: 0,// 签到天数
    init() {
        this._guided = G.data.getItem(StorageKey.GUIDE, false);
        this._diamond = G.data.getItem(StorageKey.DIAMOND, 0);
        this._currCoin = G.data.getItem(StorageKey.CURR_COIN, '0');
        this._allCon = G.data.getItem(StorageKey.ALL_COIN, '0');
        this._currAnimalIndex = G.data.getItem(StorageKey.CURR_ANIMAL_INDEX, 0);
        this._maxAnimalCount = G.data.getItem(StorageKey.MAX_ANIMAL_COUNT, 20);
        this._animalBuyCount = G.data.getItem(StorageKey.ANIMAL_BUY_COUNT, []);

        this._initSign();
        this._getFreeCoin();
        this._initToadyData();
        G.utils.schedule(() => {
            this._saveCoin();
            this.resetOutLineTime();
        }, 10)
    },

    getOutLineTime() {
        let date = G.data.getItem(StorageKey.OUT_LINE_TIME, Date.now());
        return date;
    },
    resetOutLineTime() {
        G.data.setItem(StorageKey.OUT_LINE_TIME, Date.now());
    },

    getGuided() {
        return this._guided;
    },
    setGuided() {
        this._guided = true;
        G.data.setItem(StorageKey.GUIDE, true);
    },

    _initToadyData() {
        let param = G.data.getItem(StorageKey.TOADY_AD_DIAMOND_COUNT, { data: 0, date: Date.now() });
        if (G.common.isToday(param.date)) {
            this._todayAdDiamondCount = param.data;
        } else {
            this._todayAdDiamondCount = 0;
        }

        param = G.data.getItem(StorageKey.TOADY_SHARE_DIAMOND_COUNT, { data: 0, date: Date.now() });
        if (G.common.isToday(param.date)) {
            this._todayShareDiamondCount = param.data;
        } else {
            this._todayShareDiamondCount = 0;
        }

        param = G.data.getItem(StorageKey.TOADY_SHARE_ENERGY_COUNT, { data: 0, date: Date.now() });
        if (G.common.isToday(param.date)) {
            this._todayShareEnergyCount = param.data;
        } else {
            this._todayShareEnergyCount = 0;
        }
    },

    getCanGetAdDiamond() {
        return this._todayAdDiamondCount < MAX_TOADY_AD_DIAMOND_COUNT;
    },
    addToadyAdDiamondCount() {
        this._todayAdDiamondCount++;
        G.data.setItem(StorageKey.TOADY_AD_DIAMOND_COUNT, { data: this._todayAdDiamondCount, date: Date.now() });
    },

    getCanGetShareDiamond() {
        return this._todayShareDiamondCount < MAX_TOADY_SHARE_DIAMOND_COUNT;
    },
    addToadyShareDiamondCount() {
        this._todayShareDiamondCount++;
        G.data.setItem(StorageKey.TOADY_SHARE_DIAMOND_COUNT, { data: this._todayShareDiamondCount, date: Date.now() });
    },

    getCanGetShareEnergy() {
        return this._todayShareEnergyCount < MAX_TOADY_SHARE_ENERGY_COUNT;
    },
    addToadyShareEnergyCount() {
        this._todayShareEnergyCount++;
        G.data.setItem(StorageKey.TOADY_SHARE_ENERGY_COUNT, { data: this._todayShareEnergyCount, date: Date.now() });
    },

    _initSign() {
        let data = G.data.getItem(StorageKey.SIGN_DAY, { count: 0, date: 0 });
        let date = data.date;
        this._signDay = data.count;
        if (G.common.isToday(date)) {
            this._hadSigned = true;
        } else {
            this._hadSigned = false;
        }
    },

    getHadSigned() {
        return this._hadSigned;
    },

    getSignDay() {
        return this._signDay;
    },
    addSignDay() {
        this._signDay++;
        this._hadSigned = true;
        G.data.setItem(StorageKey.SIGN_DAY, { count: this._signDay, date: Date.now() });
    },

    _getFreeCoin() {
        let hadAchieveFreeCoin = G.data.getItem(StorageKey.FREE_COIN, false);
        if (hadAchieveFreeCoin) {
            return;
        }
        this.addCoin(1000);
        G.data.setItem(StorageKey.FREE_COIN, true);
    },

    _saveCoin() {
        let coin;
        let allCoin;
        if (typeof this._currCoin == 'string') {
            coin = this._currCoin;
        } else {
            coin = this._currCoin.toString();
        }
        if (typeof this._allCon == 'string') {
            allCoin = this._allCon;
        } else {
            allCoin = this._allCon.toString();
        }
        G.data.setItem(StorageKey.CURR_COIN, coin);
        G.data.setItem(StorageKey.ALL_COIN, allCoin);
    },

    _saveAliveAnimal() {
        let param = [];
        for (let i = 0; i < this._aliveAnimalData.length; i++) {
            let data = {
                index: this._aliveAnimalData[i].getIndex()
            };
            param.push(data);
        }
        G.data.setItem(StorageKey.ALIVE_ANIMAL_DATA, param);
    },

    readAliveAnimal() {
        return G.data.getItem(StorageKey.ALIVE_ANIMAL_DATA, []);
    },

    getAnimalBuyCount(index) {
        return this._animalBuyCount[index] || 0;
    },

    addAnimalBuyCount(index) {
        let count = this._animalBuyCount[index] || 0;
        this._animalBuyCount[index] = count + 1;
        G.data.setItem(StorageKey.ANIMAL_BUY_COUNT, this._animalBuyCount);
    },

    getAliveAnimalData() {
        return this._aliveAnimalData;
    },

    addAliveAnimal(animal) {
        this._aliveAnimalData.push(animal);
        GameState.updateProductionSpeed();
        this._saveAliveAnimal();
    },
    removeAliveAnimal(animal) {
        G.utils.removeFromArr(animal, this._aliveAnimalData);
        GameState.updateProductionSpeed();
        this._saveAliveAnimal();
    },

    getDiamond() {
        return this._diamond;
    },

    addDiamond(count) {
        this._diamond += count;
        G.data.setItem(StorageKey.DIAMOND, this._diamond);
        G.onFire.fire(EventKey.EVENT_UPDATE_DIAMOND);
    },

    getCurrCoin() {
        return this._currCoin;
    },

    addCoin(count) {
        this._currCoin = G.bignumber.plus(this._currCoin, count);
        this._allCon = G.bignumber.plus(this._allCon, count);

        G.onFire.fire(EventKey.EVENT_UPDATE_COIN);
        // this._saveCoin();

        this.subScore({ score: G.bignumber.toUnitType(this._allCon) });
    },
    subCoin(count) {
        this._currCoin = G.bignumber.minus(this._currCoin, count);

        G.onFire.fire(EventKey.EVENT_UPDATE_COIN);
        // this._saveCoin();
    },

    subScore(score) {
        G.partner.setUserCloudStorage(OtherKey.kRecord, score);
    },

    getAllCoin() {
        return this._allCon;
    },

    getCurrAnimalIndex() {
        return this._currAnimalIndex;
    },
    setCurrAnimalIndex(index) {
        this._currAnimalIndex = index;
        G.data.setItem(StorageKey.CURR_ANIMAL_INDEX, this._currAnimalIndex);
    },



    getMaxAnimalCount() {
        return this._maxAnimalCount;
    },
    addMaxAnimalCount(count) {
        this._maxAnimalCount += count;
        G.data.setItem(StorageKey.MAX_ANIMAL_COUNT, this._maxAnimalCount);
    },
}